Ext.define('Admin.view.emergencyDispatch.EmergencyIncidentMgtController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.emergencyIncidentMgt',

    init: function() {

    },

    autoLoad: true,

    //应急事件查询
    onWarnListSearchClick: function() {
        var emergencyIncidentGrid = this.lookupReference('emergencyIncidentGrid');
        var store = emergencyIncidentGrid.getStore();
        var WARN_EVENTP = this.lookupReference('WARN_EVENTP').getValue();
        var WARN_LOC = this.lookupReference('WARN_LOC').getValue();
        var WARN_PLS = this.lookupReference('WARN_PLS').getValue();
        var WARN_STARTTM = this.lookupReference('WARN_STARTTM').getValue();
        var WARN_ENDTM = this.lookupReference('WARN_ENDTM').getValue();
        if (WARN_STARTTM > WARN_ENDTM) {
            Ext.Msg.alert('提示', '起始计算时间需要小于结束计算时间'); //这种方式非常常见的
            return;
        }
        store.getProxy().extraParams = {
            river: config.RIVER,
            evenTp: WARN_EVENTP,
            rcd: WARN_LOC,
            plS: WARN_PLS,
            startTime: Ext.Date.format(WARN_STARTTM, "Y-m-d H:i:s"),
            endTime: Ext.Date.format(WARN_ENDTM, "Y-m-d H:i:s"),
        };
        store.reload();
    },

    //删除动作
    wanrListDel: function(grid, rowIndex, colIndex) {
        var evenCd = grid.getStore().getAt(rowIndex).get('evenCd');
        Ext.Msg.confirm("警告", "删除应急事件将同时删除相关的应急方案,确定要删除吗？", function(button) {
            if (button === 'yes') {
                Ext.Ajax.request({
                    url: config.URL + 'emergency/ddsEdEvent/delete',
                    method: "post",
                    success: function(response, opts) {
                        Ext.MessageBox.hide();
                        var result = Ext.JSON.decode(response.responseText);
                        if (result.code === 1) {
                            grid.store.load();
                            Ext.Msg.alert('系统提示', "删除成功");
                        } else {
                            Ext.Msg.alert("系统提示", result.msg);
                        }
                    },
                    failure: function() {
                        Ext.Msg.alert('系统提示', '系统出错！');
                    },
                    params: { evenCd: evenCd, river: config.RIVER }
                });
            }
        });

    },


    //新增应急事件
    onManualReportClick: function(grid, rowIndex, colIndex) {
        var _that = this;
        var win = this.lookupReference('popupAddWindow');
        if (!win) {
            win = Ext.create('Ext.window.Window', {
                title: '人工填报',
                width: 530,
                //				modal: false,
                layout: 'fit',
                closeAction: 'hide',
                reference: 'popupAddWindow',
                buttons: [{
                        text: '保存',
                        handler: function() {
                            // var form = _that.lookupReference('eventSaveForm').getForm();
                            var form = win.items.items[0].getForm();
                            if (form.isValid()) { // 表单验证是否通过
                                var modal = this.up('window');
                                var emergencyIncidentGrid = _that.lookupReference('emergencyIncidentGrid');
                                var store = emergencyIncidentGrid.getStore();
                                var values = form.getValues();
                                values.river = config.RIVER;
                                values.pubTp = "20";
                                Ext.Ajax.request({
                                    url: config.URL + 'emergency/ddsEdEvent/save',
                                    method: "post",
                                    success: function(response, opts) {
                                        var result = Ext.JSON.decode(response.responseText);
                                        Ext.MessageBox.hide();
                                        if (result.code === 1) {
                                            Ext.Msg.alert('系统提示', "保存成功");
                                            store.reload();
                                            modal.hide();
                                        } else {
                                            Ext.Msg.alert("系统提示", result.msg);
                                        }
                                    },
                                    failure: function() {
                                        Ext.Msg.alert('系统提示', '系统出错！');
                                    },
                                    params: values
                                });
                            } else {
                                Ext.Msg.alert('提示', '表单填写不合法！');
                            }

                        }
                    },
                    {
                        text: '取消',
                        handler: function() {
                            var modal = this.up('window');
                            modal.hide();
                        }
                    },
                ],
                items: { // Let's put an empty grid in just to illustrate fit layout
                    xtype: 'manualReport',
                    border: false,
                    reference: 'eventSaveForm'
                }
            });
            // this.getView().add(win)
        }
        win.show();
    },


    //修改应急事件
    onWarnListUpData: function(grid, rowIndex, colIndex) {
        var rec = grid.getStore().getAt(rowIndex);
        var _that = this;
        var win = this.lookupReference('popupUpdataWindow');
        var saveUpdata = function() {
            // var form = _that.lookupReference('eventUpdataForm').getForm();
            var form = win.items.items[0].getForm();
            // console.log(form.getValues());
            if (form.isValid()) {
                var modal = this.up('window');
                var emergencyIncidentGrid = _that.lookupReference('emergencyIncidentGrid');
                var store = emergencyIncidentGrid.getStore();
                var values = form.getValues();
                values.pubTp = "20";
                values.river = config.RIVER;
                Ext.Ajax.request({
                    url: config.URL + 'emergency/ddsEdEvent/update',
                    method: "post",
                    success: function(response, opts) {
                        Ext.MessageBox.hide();
                        var result = Ext.JSON.decode(response.responseText);
                        if (result.code === 1) {
                            Ext.Msg.alert('系统提示', "修改成功");
                            store.reload();
                            modal.hide();
                        } else {
                            Ext.Msg.alert("系统提示", result.msg);
                        }
                    },
                    failure: function() {
                        Ext.Msg.alert('系统提示', '系统出错！');
                    },
                    params: values
                });
            } else {
                Ext.Msg.alert('提示', '表单填写不合法！');
            }
        };
        if (!win) {
            win = Ext.create('Ext.window.Window', {
                title: '修改应急事件',
                width: 530,
                //				modal: false,
                layout: 'fit',
                closeAction: 'hide',
                reference: 'popupUpdataWindow',
                buttons: [{
                        text: '保存',
                        handler: saveUpdata
                    },
                    {
                        text: '取消',
                        handler: function() {
                            var modal = this.up('window');
                            // _that.lookupReference('eventUpdataForm').getForm().reset();
                            win.items.items[0].getForm().reset();
                            modal.hide();
                        }
                    },

                ],
                items: {
                    xtype: 'manualReport',
                    border: false,
                    reference: 'eventUpdataForm'
                }
            });
            // this.getView().add(win);
        }
        win.show();
        console.log(rec.data);
        // console.log(win.items.items[0].getForm());
        win.items.items[0].getForm().reset();
        win.items.items[0].getForm().setValues(rec.data);
        // _that.lookupReference('eventUpdataForm').getForm().reset();
        // _that.lookupReference('eventUpdataForm').getForm().setValues(rec.data)

    },


    onWarningRuleSetClick: function() {
        var win = this.lookupReference('onWarningRuleWindow');
        if (!win) {
            win = Ext.create('Ext.window.Window', {
                title: '预警规则设置',
                width: 960,
                reference: 'onWarningRuleWindow',
                layout: 'fit',
                items: {
                    xtype: 'warningRuleSet',
                    border: false
                }
            }).show();
        }
    },
    //预警信息查询
    inforSearch: function() {
        var meetingGrid = this.lookupReference('meetingGrid');
        var store = meetingGrid.getStore();
        var INFOR_NAME = this.lookupReference('INFOR_NAME').getValue();
        store.getProxy().extraParams = {
            river: config.RIVER,
            secCd: INFOR_NAME,
        };
        store.reload();
    },

    onGridCellItemClick: function(view, td, cellIndex, record) {
        if (cellIndex === 6) {
            var sjlx = record.get('sjlx');
            if (sjlx == '水污染') {
                var fbfs = record.get('fbfs');
                if (fbfs == '自动报警') {
                    this.redirectTo('contaminantsTrack');
                } else if (fbfs == '人工填报') {

                }
            }
        }
    },

    //忽略预警信息
    changemeetingGrid: function(grid, rowIndex, colIndex) {
        var that = this;
        var secCd = grid.getStore().getAt(rowIndex).get('secCd');
        var tm = grid.getStore().getAt(rowIndex).get('tm');
        var label = grid.getStore().getAt(rowIndex).get('label');

        Ext.Msg.confirm("警告", "确定要忽略吗？", function(button) {
            if (button === 'yes') {
                Ext.Ajax.request({
                    url: config.URL + "emergency/ddsEdWarn/updateFlag",
                    method: "post",
                    success: function(response, opts) {
                        Ext.MessageBox.hide();
                        var result = Ext.JSON.decode(response.responseText);
                        if (result.code === 1) {
                            grid.store.load();
                            Ext.Msg.alert('系统提示', "忽略成功");
                        } else {
                            Ext.Msg.alert("系统提示", result.msg);
                        }
                    },
                    failure: function() {
                        Ext.Msg.alert('系统提示', '系统出错！');
                    },
                    params: { tm: tm, flag: '3', label: label, secCd: secCd, river: config.RIVER }
                });
            }
        });

    },


    //确认预警信息
    delmeetingGrid: function(grid, rowIndex, colIndex) {
        var that = this;
        var secCd = grid.getStore().getAt(rowIndex).get('secCd');
        var tm = grid.getStore().getAt(rowIndex).get('tm');
        var label = grid.getStore().getAt(rowIndex).get('label');

        // Ext.Msg.confirm("警告", "确定要忽略吗？", function (button) {
        // 	if (button === 'yes') {
        Ext.Ajax.request({
            url: config.URL + "emergency/ddsEdWarn/updateFlag",
            method: "post",
            success: function(response, opts) {
                Ext.MessageBox.hide();
                var result = Ext.JSON.decode(response.responseText);
                if (result.code === 1) {
                    grid.store.load();
                    Ext.Msg.alert('系统提示', "确认成功");
                } else {
                    Ext.Msg.alert("系统提示", result.msg);
                }
            },
            failure: function() {
                Ext.Msg.alert('系统提示', '系统出错！');
            },
            params: { tm: tm, flag: '2', label: label, secCd: secCd, river: config.RIVER, pubTp: "10" }
        });
        // 	}
        // })
    },



    //新增预警规则
    addwarningRuleSet: function() {
        var that = this;
        var grid = that.lookupReference('baseGridPanel');
        var store = grid.getStore();

        var win = this.lookupReference('addwarningRuleSetWindow');
        if (!win) {
            win = Ext.create('Ext.window.Window', {
                title: '添加预警规则',
                width: 530,
                layout: 'fit',
                resizable: false,
                // closeAction: 'hide',
                items: { // Let's put an empty grid in just to illustrate fit layout
                    xtype: 'form',
                    reference: 'addwarningRuleSetWindow',
                    layout: 'form',
                    border: false,
                    items: [{
                            xtype: "combobox",
                            fieldLabel: "测站名称",
                            displayField: 'stNm',
                            queryMode: 'local',
                            valueField: 'stcd',
                            triggerAction: 'all',
                            anyMatch: true,
                            labelWidth: 60,
                            allowBlank: false,
                            editable: false,
                            name: 'stcd',
                            listeners: {
                                added: function(combo) {
                                    var store = Ext.create("Ext.data.Store", {
                                        fields: ["stNm", "stcd", 'stTp'],
                                        data: statList
                                    });
                                    store.filter({
		                                property: 'river',
		                                value: config.RIVER
		                            });
                                    combo.setStore(store);
                                }
                            }
                        },
                        {
                            xtype: "combobox",
                            fieldLabel: "预警指标",
                            displayField: 'label',
                            queryMode: 'local',
                            valueField: 'value',
                            triggerAction: 'all',
                            anyMatch: true,
                            labelWidth: 60,
                            allowBlank: false,
                            editable: false,
                            name: 'types',
                            listeners: {
                                added: function(combo) {
                                    var store = Ext.create("Ext.data.Store", {
                                        fields: ["label", "value"],
                                        data: [
                                            { label: "水位", value: "Z" },
                                            { label: "流量", value: "Q" },
                                            { label: "BOD", value: "BOD" },
                                            { label: "COD", value: "COD" },
                                            { label: "TP", value: "TP" },
                                            { label: "TN", value: "TN" },
                                            { label: "氨氮", value: "NH3N" }
                                        ]
                                    });
                                    combo.setStore(store);
                                }
                            }
                        },
                        {
                            xtype: "combobox",
                            fieldLabel: "单位",
                            displayField: 'label',
                            queryMode: 'local',
                            valueField: 'value',
                            triggerAction: 'all',
                            // allowBlank: false,
                            editable: false,
                            labelWidth: 60,
                            name: 'unitCode',
                            listeners: {
                                added: function(combo) {
                                    var store = Ext.create("Ext.data.Store", {
                                        fields: ["label", "value"],
                                        data: [
                                            { label: "m", value: "m" },
                                            { label: "m³/s", value: "m³/s" },
                                            { label: "mg/L", value: "mg/L" }
                                        ]
                                    });
                                    combo.setStore(store);
                                }
                            }
                        },
                        {
                            fieldLabel: '上下限标识',
                            xtype: "combobox",
                            displayField: 'label',
                            valueField: 'value',
                            listeners: {
                                added: function(combo) {
                                    var store = Ext.create("Ext.data.Store", {
                                        autoLoad: true,
                                        data: [{ label: '上限', value: 0 }, { label: '下限', value: 1 }]
                                    });
                                    combo.setStore(store);
                                }
                            },
                            allowBlank: false,
                            editable: false,
                            name: 'updown'
                        }, {
                            fieldLabel: '级别',
                            xtype: "combobox",
                            displayField: 'label',
                            valueField: 'value',
                            listeners: {
                                added: function(combo) {
                                    var store = Ext.create("Ext.data.Store", {
                                        autoLoad: true,
                                        data: [
                                            { label: 'I级预警', value: 1 },
                                            { label: 'II级预警', value: 2 },
                                            { label: 'III级预警', value: 3 },
                                            { label: 'IV级预警', value: 4 }
                                        ]
                                    });
                                    combo.setStore(store);
                                }
                            },
                            allowBlank: false,
                            editable: false,
                            name: 'levels'
                        }, {
                            xtype: 'numberfield',
                            fieldLabel: '指标值',
                            // allowBlank: false,
                            name: 'vavule'
                        }
                    ]
                },
                buttons: [{
                        text: '保存',
                        handler: function() {
                            var form = that.lookupReference('addwarningRuleSetWindow').getForm();

                            if (form.isValid()) { // 表单验证是否通过
                                var modal = this.up('window');
                                var baseGridPanel = that.lookupReference('baseGridPanel');
                                var store = baseGridPanel.getStore();
                                // console.log(store)
                                var values = form.getValues();
                                values.river = config.RIVER;
                                console.log(values);
                                Ext.Ajax.request({
                                    url: config.URL + 'emergency/ddsEdWrule/save',
                                    method: "post",
                                    success: function(response, opts) {
                                        var result = Ext.JSON.decode(response.responseText);
                                        Ext.MessageBox.hide();
                                        if (result.code === 1) {
                                            that.baseGridPaneladded();
                                            Ext.Msg.alert('系统提示', "新增成功");
                                            modal.close();
                                        } else {
                                            Ext.Msg.alert("系统提示", result.msg);
                                        }
                                    },
                                    failure: function() {
                                        Ext.Msg.alert('系统提示', '系统出错！');
                                    },
                                    params: values
                                });
                            } else {
                                Ext.Msg.alert('提示', '表单填写不合法！');
                            }
                        }
                    },
                    {
                        text: '取消',
                        handler: function() {
                            var modal = this.up('window');
                            modal.close();
                        }
                    },
                ]
            });
            this.getView().add(win);
        }
        win.show();

    },
    //保存预警规则
    savewarningRuleSet: function() {
        var that = this;
        var grid = that.lookupReference('baseGridPanel');
        console.log(grid.getStore());
    },

    //预警规则数据加载
    baseGridPaneladded: function() {
        var that = this;
        var baseGridPanel = that.lookupReference('baseGridPanel');
        // var store = baseGridPanel.getStore();
        var store = Ext.create("Ext.data.Store", {
            autoLoad: true,
        });
        Ext.Ajax.request({
            // url: config.URL + "emergency/ddsEdWarn/list",
            url: config.URL + "emergency/ddsEdWrule/list",
            method: "post",
            success: function(response, opts) {
                Ext.MessageBox.hide();
                var result = Ext.JSON.decode(response.responseText);
                var data = result.list;

                if (result.list.length > 0) {
                    for (var i = 0; i < data.length; i++) {
                        data[i].levels1 = '';
                        data[i].levels2 = '';
                        data[i].levels3 = '';
                        data[i].levels4 = '';
                        for (var j = 0; j < data[i].wruleList.length; j++) {
                            switch (data[i].wruleList[j].levels) {
                                case 1:
                                    data[i].levels1 = (data[i].wruleList[j].vavule > -1) ? data[i].wruleList[j].vavule : '';
                                    break;
                                case 2:
                                    data[i].levels2 = (data[i].wruleList[j].vavule > -1) ? data[i].wruleList[j].vavule : '';
                                    break;
                                case 3:
                                    data[i].levels3 = (data[i].wruleList[j].vavule > -1) ? data[i].wruleList[j].vavule : '';
                                    break;
                                case 4:
                                    data[i].levels4 = (data[i].wruleList[j].vavule > -1) ? data[i].wruleList[j].vavule : '';
                                    break;
                            }
                        }
                    }
                    store.loadData(data);
                    baseGridPanel.setStore(store);
                } else {
                    store.loadData([]);
                    Ext.Msg.alert("系统提示", result.msg);
                }
            },
            failure: function() {
                Ext.Msg.alert('系统提示', '系统出错！');
                store.loadData([]);
            },
            params: { river: config.RIVER }
        });
    },
    //预警规则设置單元格修改
    warningRuleSetGridEdit: function(editor, context, eOpts) {
        var that = this;
        // console.log(context);
        var value = Ext.pluck(context.store.data.items, 'data');
        var changeData = value[context.rowIdx];
        setTimeout(function() {
            var values = {};
            values.stcd = changeData.stcd;
            values.types = changeData.types;
            values.unitCode = changeData.unitCode;
            values.updown = changeData.updown ? changeData.updown : 0;
            values.river = config.RIVER;
            if (context.colIdx > 3) {
                switch (context.colIdx) {
                    case 4:
                        values.levels = 4;
                        values.vavule = changeData.levels4;
                        break;
                    case 5:
                        values.levels = 3;
                        values.vavule = changeData.levels3;
                        break;
                    case 6:
                        values.levels = 2;
                        values.vavule = changeData.levels2;
                        break;
                    case 7:
                        values.levels = 1;
                        values.vavule = changeData.levels1;
                        break;
                }
            }
            // console.log(changeData);
            // console.log(values);

            var state; //返回的狀態嘛
            Ext.Ajax.request({
                url: config.URL + "emergency/ddsEdWrule/save",
                method: "post",
                success: function(response, opts) {
                    Ext.MessageBox.hide();
                    var result = Ext.JSON.decode(response.responseText);
                    if (result.code === 1) {
                        state = true;
                        that.baseGridPaneladded();
                        Ext.toast({
                            html: result.msg,
                            width: 200,
                            align: 't'
                        });
                    } else {
                        state = false;
                        that.baseGridPaneladded();
                        Ext.toast({
                            html: result.msg,
                            width: 200,
                            align: 't'
                        });
                    }
                },
                failure: function() {
                    state = false;
                    that.baseGridPaneladded();
                    Ext.Msg.alert("系统提示", "系统出错！");
                },
                params: values
            });
            return state;
        }, 10);

    },
    //删除预警规则
    // baseGridPanelDel: function (grid, rowIndex, colIndex) {
    // 	var that = this;
    // 	var stcd = grid.getStore().getAt(rowIndex).get('stcd');
    // 	var types = grid.getStore().getAt(rowIndex).get('types');
    // 	Ext.Msg.confirm("警告", "确定要删除吗？", function (button) {
    // 		if (button === 'yes') {
    // 			Ext.Ajax.request({
    // 				url: config.URL + 'emergency/ddsEdWrule/delete',
    // 				method: "post",
    // 				success: function (response, opts) {
    // 					Ext.MessageBox.hide();
    // 					var result = Ext.JSON.decode(response.responseText);
    // 					if (result.code === 1) {
    // 						// grid.store.load();
    // 						that.baseGridPaneladded();
    // 						Ext.Msg.alert('系统提示', "删除成功");
    // 					} else {
    // 						Ext.Msg.alert("系统提示", result.msg);
    // 					}
    // 				},
    // 				failure: function () {
    // 					Ext.Msg.alert('系统提示', '系统出错！');
    // 				},
    // 				params: { types:types,stcd: stcd,river:config.RIVER}
    // 			});
    // 		}
    // 	})
    // }
});